import { useEffect,useState } from "react";
import styled from "./index.less";
import cn from "classnames";
import dayjs from 'dayjs';

import {getPerformanceInformation} from "../service";

// 业绩信息
type InfoTableProps = {
  innerCode?: string; // 用于接口查询
  start?:string;
  end?:string;
  className?:any;
  type:string;//周 月 自定义
  onChangeLoading:(bool:boolean)=>void;
  PDF?:boolean;
};
export const InfoTable = (props: InfoTableProps) => {
  const { innerCode,start,end ,className,type,onChangeLoading,PDF} = props;
  const [info, setInfo] = useState<any>({}); // 业绩信息

  useEffect(() => {
    if (innerCode && start && end) {
      onChangeLoading&&onChangeLoading(false);
      getPerformanceInformation({
        innerCode,
        start:dayjs(start).format("YYYY-MM-DD"),
        end:dayjs(end).format("YYYY-MM-DD")
      }).then(res=>{
        if(res?.code==="0" &&res?.data){
          setInfo(res?.data);
        }else{
          setInfo({});
        }
        onChangeLoading&&onChangeLoading(true);
      })
    }
  }, [innerCode, start, end]);

  return (
    <table
      style={{ tableLayout: "fixed", minWidth: "100%" }}
      className={cn(
        styled.cusTable_convert,
        PDF?styled.pdf_table_container:undefined,
        className,
      )}
    >
      {/* <colgroup>
        </colgroup> */}
      <tbody>
        <tr>
          <td className={styled.label} colSpan={1}>
            {/* 最新净值({end?dayjs(end).format("YYYY-MM-DD"):"-"}) */}
            最新净值({info?.recentUnitNVDate? dayjs(info?.recentUnitNVDate).format("YYYY-MM-DD"):"-"})
          </td>
          <td className={styled.value} colSpan={2} title={info?.recentUnitNV||info?.recentUnitNV===0?info?.recentUnitNV+"元":'-'}>
            {info?.recentUnitNV||info?.recentUnitNV===0?info?.recentUnitNV+"元":'-'}
          </td>
          <td className={styled.label} colSpan={1}>
            {type=="1"?"本周":type=="2"?"本月":"区间"}回购利率
          </td>
          <td className={styled.value} colSpan={2} title={info?.intervalRepoRate||info?.intervalRepoRate===0?info?.intervalRepoRate+"%":'-'}>
            {info?.intervalRepoRate||info?.intervalRepoRate===0?info?.intervalRepoRate+"%":'-'}
          </td>
          <td className={styled.label} colSpan={1}>
            累计运行天数
          </td>
          <td className={styled.value} colSpan={2} title={info?.runDay||info?.runDay===0?info?.runDay+"天":'-'}>
            {info?.runDay||info?.runDay===0?info?.runDay+"天":'-'}
          </td>
        </tr>
        <tr>
          <td className={styled.label} colSpan={1}>
            今年以来年化收益
          </td>
          <td
            className={styled.value}
            colSpan={2}
            title={info?.yearAnnualizedIncome||info?.yearAnnualizedIncome===0?info?.yearAnnualizedIncome+"%":'-'}
          >
            {info?.yearAnnualizedIncome||info?.yearAnnualizedIncome===0?info?.yearAnnualizedIncome+"%":'-'}
          </td>
          <td className={styled.label} colSpan={1}>
            今年以来加权
          </td>
          <td className={styled.value} colSpan={2} title={info?.yearRate||info?.yearRate===0?info?.yearRate+"%":'-'}
>
            {info?.yearRate||info?.yearRate===0?info?.yearRate+"%":'-'}

          </td>
          <td className={styled.label} colSpan={1}>
            累计回购笔数
          </td>
          <td className={styled.value} colSpan={2} title={info?.establishCount||info?.establishCount===0?info?.establishCount+"笔":'-'}>
            {info?.establishCount||info?.establishCount===0?info?.establishCount+"笔":'-'}

          </td>
        </tr>
        <tr>
          <td className={styled.label} colSpan={1}>
            成立以来年化收益
          </td>
          <td className={styled.value} colSpan={2} title={info?.establishAnnualizedIncome||info?.establishAnnualizedIncome===0?info?.establishAnnualizedIncome+"%":'-'}>
            {info?.establishAnnualizedIncome||info?.establishAnnualizedIncome===0?info?.establishAnnualizedIncome+"%":'-'}

          </td>
          <td className={styled.label} colSpan={1}>
            成立以来加权
          </td>
          <td className={styled.value} colSpan={2} title={info?.establishRate||info?.establishRate===0?info?.establishRate+"%":'-'}>
            {info?.establishRate||info?.establishRate===0?info?.establishRate+"%":'-'}

          </td>
          <td className={styled.label} colSpan={1}>
            累计回购金额
          </td>
          <td className={styled.value} colSpan={2} title={info?.establishRepoAmount||info?.establishRepoAmount===0?info?.establishRepoAmount+"亿元":'-'}>
            {info?.establishRepoAmount||info?.establishRepoAmount===0?info?.establishRepoAmount+"亿元":'-'}

          </td>
          
        </tr>
        
        
      </tbody>
    </table>
  );
};